package com.xyht.sca_s.student_manage_system.modules.appDynamic.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.req.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.resp.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.mapper.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.service.AppProcessNodeConfigService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.appDynamic.constant.ApproverTypeConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.appDynamic.constant.ConditionControlSubTypeConstant.CONDITION_SUBTYPE_SUBMITTER;
import static com.xyht.sca_s.student_manage_system.modules.appDynamic.constant.ConditionControlSubTypeConstant.CONDITION_SUBTYPE_SWITCH;
import static com.xyht.sca_s.student_manage_system.modules.appDynamic.constant.ControlTypeConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.appDynamic.constant.NodeTypeConstant.*;

/**
 * <p>
 * 流程节点配置表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-07-26
 */
@Service
public class AppProcessNodeConfigServiceImpl extends ServiceImpl<AppProcessNodeConfigMapper, AppProcessNodeConfig> implements AppProcessNodeConfigService {
    @Resource
    private AppProcessNodeConfigMapper appProcessNodeConfigMapper;
    @Resource
    private AppControlOrderMapper appControlOrderMapper;
    @Resource
    private AppBasicInfoMapper appBasicInfoMapper;
    @Resource
    private AppTextControlConfigMapper appTextControlConfigMapper;
    @Resource
    private AppTimeControlConfigMapper appTimeControlConfigMapper;
    @Resource
    private AppOptionControlConfigMapper appOptionControlConfigMapper;
    @Resource
    private AppFileControlConfigMapper appFileControlConfigMapper;
    @Resource
    private AppStepperControlConfigMapper appStepperControlConfigMapper;
    @Resource
    private SmsRoleMapper smsRoleMapper;
    @Resource
    private SmsUserMapper smsUserMapper;
    @Resource
    private AppConditionGroupMapper appConditionGroupMapper;
    @Resource
    private AppConditionMapper appConditionMapper;

    @Override
    @Transactional
    public ResponseResult addProcessNode(AppProcessNodeConfigReq nodeReq) {
        //校验参数
        if ((nodeReq.getType() != APPROVE_NODE && nodeReq.getType() != COPY_NODE) || nodeReq.getPrevId().isEmpty() || nodeReq.getNextId().isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        String prevId = nodeReq.getPrevId().get(0);
        String nextId = nodeReq.getNextId().get(0);

        //获取父节点及其子idList
        AppProcessNodeConfig prevNode = appProcessNodeConfigMapper.selectById(prevId);
        if (isNullOrEmpty(prevNode)) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        List<String> prevNodeSonIdList = JSON.parseArray(prevNode.getNextId(), String.class);
        //校验传入父子节点是否相邻
        if (!(prevNodeSonIdList.contains(nextId))) {
            return CommonResult.failed(CommonCodeEnum.PREV_AND_NEXT_NODE_NOT_NEAR);
        }
        //获取子节点及其父idList
        AppProcessNodeConfig nextNode = appProcessNodeConfigMapper.selectById(nextId);
        if (isNullOrEmpty(nextNode)) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        List<String> nextNodeFatherIdList = JSON.parseArray(nextNode.getPrevId(), String.class);
        //校验传入父子节点是否相邻
        if (!(nextNodeFatherIdList.contains(prevId))) {
            return CommonResult.failed(CommonCodeEnum.PREV_AND_NEXT_NODE_NOT_NEAR);
        }

        //添加当前节点
        AppProcessNodeConfig node = new AppProcessNodeConfig();
        if (nodeReq.getType() == APPROVE_NODE) {
            node = initializeApproverNode(prevNode.getAppId());
        } else if (nodeReq.getType() == COPY_NODE) {
            node = initializeCopyNode(prevNode.getAppId());
        }

        ArrayList<String> prevIdList = new ArrayList<>();
        prevIdList.add(prevId);
        node.setPrevId(JSON.toJSONString(prevIdList));
        ArrayList<String> nextIdList = new ArrayList<>();
        nextIdList.add(nextId);
        node.setNextId(JSON.toJSONString(nextIdList));
        appProcessNodeConfigMapper.updateById(node);

        //修改父节点
        Collections.replaceAll(prevNodeSonIdList, nextId, node.getId());
        prevNode.setNextId(JSON.toJSONString(prevNodeSonIdList));
        appProcessNodeConfigMapper.updateById(prevNode);

        //修改子节点
        Collections.replaceAll(nextNodeFatherIdList, prevId, node.getId());
        nextNode.setPrevId(JSON.toJSONString(nextNodeFatherIdList));
        appProcessNodeConfigMapper.updateById(nextNode);

        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult deleteProcessNode(ProcessNodeIdReq nodeIdReq) {
        //校验参数
        if (isNullOrEmpty(nodeIdReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        String processNodeId = nodeIdReq.getId();
        //获取当前节点,并校验当前节点是否为 申请人 或 抄送人
        AppProcessNodeConfig node = appProcessNodeConfigMapper.selectById(processNodeId);
        if (isNullOrEmpty(node)) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        if (node.getType() != APPROVE_NODE && node.getType() != COPY_NODE) {
            return CommonResult.failed(CommonCodeEnum.CAN_NOT_DELETE_THIS_NODE);
        }
        //获取当前节点的父子节点id
        String prevId = JSON.parseArray(node.getPrevId(), String.class).get(0);
        String nextId = JSON.parseArray(node.getNextId(), String.class).get(0);

        //获取父节点及其子idList
        AppProcessNodeConfig prevNode = appProcessNodeConfigMapper.selectById(prevId);
        if (isNullOrEmpty(prevNode)) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        List<String> prevNodeSonIdList = JSON.parseArray(prevNode.getNextId(), String.class);
        //获取子节点及其父idList
        AppProcessNodeConfig nextNode = appProcessNodeConfigMapper.selectById(nextId);
        if (isNullOrEmpty(nextNode)) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        List<String> nextNodeFatherIdList = JSON.parseArray(nextNode.getPrevId(), String.class);

        //特殊情况:父节点有2个子,子节点有2个父
        if (prevNodeSonIdList.size() == 2 && nextNodeFatherIdList.size() == 2) {
            //获取上节点所连接的另外一个节点id
            prevNodeSonIdList.removeIf(id -> id.equals(node.getId()));
            String prevSonAnotherNodeId = prevNodeSonIdList.get(0);
            //获取 上上节点 及其 子idList
            AppProcessNodeConfig prevPrevNode = appProcessNodeConfigMapper.selectById(JSON.parseArray(prevNode.getPrevId(), String.class).get(0));
            List<String> prevPrevNodeSonIdList = JSON.parseArray(prevPrevNode.getNextId(), String.class);
            //更新 上节点所连接的另外一个节点 的关系
            AppProcessNodeConfig prevSonAnotherNode = appProcessNodeConfigMapper.selectById(prevSonAnotherNodeId);
            prevSonAnotherNode.setPrevId(JSON.toJSONString(Arrays.asList(prevPrevNode.getId())));
            appProcessNodeConfigMapper.updateById(prevSonAnotherNode);
            //将 上上个节点的子id中的 上节点id 替换为 另外一个节点id
            Collections.replaceAll(prevPrevNodeSonIdList, prevNode.getId(), prevSonAnotherNodeId);
            prevPrevNode.setNextId(JSON.toJSONString(prevPrevNodeSonIdList));
            appProcessNodeConfigMapper.updateById(prevPrevNode);
            //删除上节点(虚拟节点)
            appProcessNodeConfigMapper.deleteById(prevNode.getId());

            //获取下节点所连接的另外一个节点id
            nextNodeFatherIdList.removeIf(id -> id.equals(node.getId()));
            String nextFatherAnotherNodeId = nextNodeFatherIdList.get(0);
            //获取 下下节点 及其 父idList
            AppProcessNodeConfig nextNextNode = appProcessNodeConfigMapper.selectById(JSON.parseArray(nextNode.getNextId(), String.class).get(0));
            List<String> nextNextNodeFatherIdList = JSON.parseArray(nextNextNode.getPrevId(), String.class);
            //更新 下节点所连接的另外一个节点 的关系
            AppProcessNodeConfig nextFatherAnotherNode = appProcessNodeConfigMapper.selectById(nextFatherAnotherNodeId);
            nextFatherAnotherNode.setNextId(JSON.toJSONString(Arrays.asList(nextNextNode.getId())));
            appProcessNodeConfigMapper.updateById(nextFatherAnotherNode);
            //将 下下个节点的父id中的 下节点id 替换为 另外一个节点id
            Collections.replaceAll(nextNextNodeFatherIdList, nextNode.getId(), nextFatherAnotherNodeId);
            nextNextNode.setPrevId(JSON.toJSONString(nextNextNodeFatherIdList));
            appProcessNodeConfigMapper.updateById(nextNextNode);
            //删除下节点(虚拟节点)
            appProcessNodeConfigMapper.deleteById(nextNode.getId());
        } else if (prevNodeSonIdList.size() > 2 && nextNodeFatherIdList.size() > 2) {//特殊情况:父节点有2个以上子,子节点有2个以上父
            prevNodeSonIdList.removeIf(id -> id.equals(node.getId()));
            nextNodeFatherIdList.removeIf(id -> id.equals(node.getId()));
        } else {//其他情况
            Collections.replaceAll(prevNodeSonIdList, node.getId(), nextId);
            Collections.replaceAll(nextNodeFatherIdList, node.getId(), prevId);
        }

        //重新获取前节点和后节点
        prevNode = appProcessNodeConfigMapper.selectById(prevId);
        nextNode = appProcessNodeConfigMapper.selectById(nextId);
        //修改父节点
        if (!isNullOrEmpty(prevNode)) {
            prevNode.setNextId(JSON.toJSONString(prevNodeSonIdList));
            appProcessNodeConfigMapper.updateById(prevNode);
        }
        //修改子节点
        if (!isNullOrEmpty(nextNode)) {
            nextNode.setPrevId(JSON.toJSONString(nextNodeFatherIdList));
            appProcessNodeConfigMapper.updateById(nextNode);
        }
        //删除当前节点
        appProcessNodeConfigMapper.deleteById(processNodeId);

        return CommonResult.success();
    }

    @Override
    public ResponseResult editProcessNode(AppProcessNodeConfigReq nodeReq) {
        //校验参数
        if (isNullOrEmpty(nodeReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if (isNullOrEmpty(nodeReq.getName()) || isNullOrEmpty(nodeReq.getType()) || isNullOrEmpty(nodeReq.getApproverType())
                || nodeReq.getAuthority().isEmpty() || isNullOrEmpty(nodeReq.getIsDeliver()) || isNullOrEmpty(nodeReq.getIsRollback())
                || isNullOrEmpty(nodeReq.getIsSign()) || isNullOrEmpty(nodeReq.getIsStamp()) || isNullOrEmpty(nodeReq.getIsOpinion())) {
            return CommonResult.failed(CommonCodeEnum.PROCESS_NODE_NOT_SETTING_ENOUGH);
        }

        AppProcessNodeConfig node = appProcessNodeConfigMapper.selectById(nodeReq.getId());
        if (isNullOrEmpty(node) || node.getId() == null) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }

        BeanUtils.copyProperties(nodeReq, node);
        node.setAuthority(JSON.toJSONString(nodeReq.getAuthority()));

        if (nodeReq.getApproverType() == DYNAMIC_APP_ROLE || nodeReq.getApproverType() == DYNAMIC_APP_ASSIGNER) {
            if (isNullOrEmpty(node.getApproverObjectId())) {
                return CommonResult.failed(CommonCodeEnum.APPROVER_TYPE_NOT_SETTING);
            }
        }

        int result = appProcessNodeConfigMapper.updateById(node);

        if (result == 0) {
            return CommonResult.failed(CommonCodeEnum.PROCESS_NODE_SETTING_FAIL);
        }

        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult nodeConfigList(String appId) {
        //校验该应用是否存在
        AppBasicInfo appBasicInfo = appBasicInfoMapper.selectById(appId);
        if (isNullOrEmpty(appBasicInfo) || appBasicInfo.getId() == null) {
            return CommonResult.failed(CommonCodeEnum.DYNAMIC_APP_NOT_EXIST);
        }

        //查询当前应用是否有节点配置
        List<AppProcessNodeConfig> nodeList = appProcessNodeConfigMapper.selectList(new LambdaQueryWrapper<AppProcessNodeConfig>()
                .eq(AppProcessNodeConfig::getAppId, appId));
        //没有则初始化流程
        if (nodeList.isEmpty()) {
            nodeList = initializeProcessNode(appId);
        }

        //包装节点数据并返回
        List<AppProcessNodeConfigResp> nodeRespList = nodeList.stream().map(node -> {
            AppProcessNodeConfigResp nodeResp = new AppProcessNodeConfigResp();
            BeanUtils.copyProperties(node, nodeResp);
            if (!isNullOrEmpty(node.getPrevId())) {
                nodeResp.setPrevId(JSONObject.parseArray(node.getPrevId(), String.class));
            }
            if (!isNullOrEmpty(node.getNextId())) {
                nodeResp.setNextId(JSONObject.parseArray(node.getNextId(), String.class));
            }
            if (!isNullOrEmpty(node.getAuthority())) {
                nodeResp.setAuthority(JSONObject.parseArray(node.getAuthority(), AuthorityResp.class));
            }
            if (!isNullOrEmpty(node.getApproverType())) {
                if (node.getApproverType() == DYNAMIC_APP_ROLE) {
                    SmsRole role = smsRoleMapper.selectById(node.getApproverObjectId());
                    nodeResp.setObjectName(role.getRoleName());
                } else if (node.getApproverType() == DYNAMIC_APP_ASSIGNER) {
                    SmsUser user = smsUserMapper.selectById(node.getApproverObjectId());
                    nodeResp.setObjectName(user.getRealName());
                }
                nodeResp.setPrevId(JSONObject.parseArray(node.getPrevId(), String.class));
            }
            //若为条件节点需设置 条件优先级 和 是否默认条件
            if (nodeResp.getType() == CONDITION_NODE) {
                AppConditionGroup appConditionGroup = appConditionGroupMapper.selectList(new LambdaQueryWrapper<AppConditionGroup>()
                        .eq(AppConditionGroup::getNodeId, node.getId())).get(0);
                nodeResp.setPriority(appConditionGroup.getPriority());
                nodeResp.setIsDefault(appConditionGroup.getIsDefault());
            }
            return nodeResp;
        }).collect(Collectors.toList());

        return CommonResult.success(nodeRespList);
    }

    @Override
    @Transactional
    public ResponseResult addCondition(ProcessNodeIdReq nodeIdReq) {
        //校验参数
        if (isNullOrEmpty(nodeIdReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        String dummyNodeId = nodeIdReq.getId();

        //获取当前节点及其子idList
        AppProcessNodeConfig dummyNode = appProcessNodeConfigMapper.selectById(dummyNodeId);
        if (isNullOrEmpty(dummyNode) || dummyNode.getType() != DUMMY_NODE) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        List<String> dummyNodeSonIdList = JSON.parseArray(dummyNode.getNextId(), String.class);
        //子idList < 2 不可添加条件
        if (dummyNodeSonIdList.size() < 2) {
            return CommonResult.failed(CommonCodeEnum.CAN_NOT_ADD_CONDITION);
        }

        //记录当前节点(发散节点)
        AppProcessNodeConfig dummyNodeUp = dummyNode;

        //获取当前 虚拟节点(发散节点) 所对应的 虚拟节点(聚合节点)
        Stack<AppProcessNodeConfig> dummyNodeStack = new Stack<>();
        dummyNodeStack.push(dummyNode);
        while (!dummyNodeStack.empty()) {
            dummyNode = getNextNode(dummyNode);
            List<String> prevIdList = JSONObject.parseArray(dummyNode.getPrevId(), String.class);
            List<String> nextIdList = JSONObject.parseArray(dummyNode.getNextId(), String.class);
            if (nextIdList.size() > 1) {
                dummyNodeStack.push(dummyNode);
            } else if (prevIdList.size() > 1) {
                if (dummyNodeStack.size() == 1) {
                    break;
                }
                dummyNodeStack.pop();
            }
        }
        //此时 dummyNode 为 虚拟节点(发散节点) 所对应的 虚拟节点(聚合节点)

        //初始化 条件节点 及 审批节点
        AppProcessNodeConfig conditionNode = initializeConditionNode(dummyNode.getAppId(), dummyNodeId, dummyNodeSonIdList.size(), false);
        AppProcessNodeConfig node = initializeApproverNode(dummyNode.getAppId());
        //修改默认条件节点的优先级
        AppConditionGroup appConditionGroup = new AppConditionGroup();
        appConditionGroup.setPriority(dummyNodeSonIdList.size() + 1);
        LambdaQueryWrapper<AppConditionGroup> queryWrapper = new LambdaQueryWrapper<AppConditionGroup>()
                .eq(AppConditionGroup::getPrevNodeId, dummyNodeId)
                .eq(AppConditionGroup::getIsDefault, true);
        appConditionGroupMapper.update(appConditionGroup, queryWrapper);
        //更新另外的节点(默认条件)
        AppConditionGroup conditionGroup = appConditionGroupMapper.selectList(queryWrapper).get(0);
        AppProcessNodeConfig nodeConfig = new AppProcessNodeConfig();
        nodeConfig.setId(conditionGroup.getNodeId());
        nodeConfig.setName("条件" + (dummyNodeSonIdList.size() + 1));
        appProcessNodeConfigMapper.updateById(nodeConfig);

        //处理节点间关系
        conditionNode.setPrevId(JSON.toJSONString(Arrays.asList(dummyNodeUp.getId())));
        conditionNode.setNextId(JSON.toJSONString(Arrays.asList(node.getId())));
        appProcessNodeConfigMapper.updateById(conditionNode);
        node.setPrevId(JSON.toJSONString(Arrays.asList(conditionNode.getId())));
        node.setNextId(JSON.toJSONString(Arrays.asList(dummyNode.getId())));
        appProcessNodeConfigMapper.updateById(node);
        //更新发散节点的子节点集合
        List<String> dummyNodeUpNextIdList = JSON.parseArray(dummyNodeUp.getNextId(), String.class);
        dummyNodeUpNextIdList.add(conditionNode.getId());
        dummyNodeUp.setNextId(JSON.toJSONString(dummyNodeUpNextIdList));
        appProcessNodeConfigMapper.updateById(dummyNodeUp);
        //更新聚合节点的父节点集合
        List<String> dummyNodePrevIdList = JSON.parseArray(dummyNode.getPrevId(), String.class);
        dummyNodePrevIdList.add(node.getId());
        dummyNode.setPrevId(JSON.toJSONString(dummyNodePrevIdList));
        appProcessNodeConfigMapper.updateById(dummyNode);

        return CommonResult.success();
    }

    @Override
    public ResponseResult conditionNodeConfigList(String id) {
        //校验节点是否存在
        AppProcessNodeConfig conditionNode = appProcessNodeConfigMapper.selectById(id);
        if (isNullOrEmpty(conditionNode) || conditionNode.getType() != CONDITION_NODE) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }

        //构造返回数据 conditionNodeInfoResp-appConditionGroupList-conditionList
        ConditionNodeInfoResp conditionNodeInfoResp = new ConditionNodeInfoResp();
        List<AppConditionGroup> conditionGroupList = appConditionGroupMapper.selectList(new LambdaQueryWrapper<AppConditionGroup>()
                .eq(AppConditionGroup::getNodeId, id));
        if (isNullOrEmpty(conditionGroupList)) {
            return CommonResult.failed(CommonCodeEnum.APP_CONDITION_GROUP_NOT_EXIST);
        }

        //设置可选优先级
        conditionNodeInfoResp.setName(conditionNode.getName());
        List<Integer> priorityList = appConditionGroupMapper.selectList(new LambdaQueryWrapper<AppConditionGroup>()
                        .eq(AppConditionGroup::getPrevNodeId, JSON.parseArray(conditionNode.getPrevId(), String.class).get(0)))
                .stream().map(AppConditionGroup::getPriority).collect(Collectors.toList());
        List<Integer> collect = priorityList.stream().distinct().collect(Collectors.toList());
        int size = collect.size();
        collect.removeIf(priority -> priority == size);
        conditionNodeInfoResp.setPriorityList(collect);

        //构造appConditionGroupList
        List<AppConditionGroupResp> conditionGroupRespList = new ArrayList<>();
        for (AppConditionGroup appConditionGroup : conditionGroupList) {
            AppConditionGroupResp appConditionGroupResp = new AppConditionGroupResp();
            BeanUtils.copyProperties(appConditionGroup, appConditionGroupResp);

            //构造conditionList
            List<AppConditionResp> conditionRespList = new ArrayList<>();
            List<AppCondition> appConditionList = appConditionMapper.selectList(new LambdaQueryWrapper<AppCondition>()
                    .eq(AppCondition::getConditionGroupId, appConditionGroup.getId()));

            if (isNullOrEmpty(appConditionList) || appConditionList.isEmpty()) {
                //返回一个空的条件
                AppConditionResp appConditionResp = new AppConditionResp();
                conditionRespList.add(appConditionResp);
//                return CommonResult.failed(CommonCodeEnum.APP_CONDITION_NOT_EXIST);
            } else {
                //返回数据库中存储的条件
                for (AppCondition appCondition : appConditionList) {
                    AppConditionResp appConditionResp = new AppConditionResp();
                    BeanUtils.copyProperties(appCondition, appConditionResp);
                    conditionRespList.add(appConditionResp);
                }
            }

            appConditionGroupResp.setConditionList(conditionRespList);
            conditionGroupRespList.add(appConditionGroupResp);
        }

        conditionNodeInfoResp.setAppConditionGroupList(conditionGroupRespList);
        return CommonResult.success(conditionNodeInfoResp);
    }

    @Override
    @Transactional
    public ResponseResult deleteConditionNode(ProcessNodeIdReq nodeIdReq) {
        if (isNullOrEmpty(nodeIdReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        String processNodeId = nodeIdReq.getId();
        //获取当前节点进行校验
        AppProcessNodeConfig node = appProcessNodeConfigMapper.selectById(processNodeId);
        if (isNullOrEmpty(node)) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        //校验当前节点是否为 条件节点
        if (node.getType() != CONDITION_NODE) {
            return CommonResult.failed(CommonCodeEnum.CAN_NOT_DELETE_THIS_NODE);
        }
        //默认条件不可删除
        List<AppConditionGroup> conditionGroupList = appConditionGroupMapper.selectList(new LambdaQueryWrapper<AppConditionGroup>()
                .eq(AppConditionGroup::getNodeId, nodeIdReq.getId()));
        if (conditionGroupList.get(0).getIsDefault()) {
            return CommonResult.failed(CommonCodeEnum.CAN_NOT_DELETE_THIS_NODE);
        }

        //获取父节点(虚拟节点)
        AppProcessNodeConfig dummyNode = appProcessNodeConfigMapper.selectById(JSON.parseArray(node.getPrevId(), String.class).get(0));
        if (isNullOrEmpty(dummyNode)) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        //获取子节点id集合
        List<String> nextNodeIdList = JSON.parseArray(dummyNode.getNextId(), String.class);

        //记录当前节点(发散节点)
        AppProcessNodeConfig dummyNodeUp = dummyNode;
        //获取当前 虚拟节点(发散节点) 所对应的 虚拟节点(聚合节点)
        Stack<AppProcessNodeConfig> dummyNodeStack = new Stack<>();
        dummyNodeStack.push(dummyNode);
        while (!dummyNodeStack.empty()) {
            dummyNode = getNextNode(dummyNode);
            List<String> prevIdList = JSONObject.parseArray(dummyNode.getPrevId(), String.class);
            List<String> nextIdList = JSONObject.parseArray(dummyNode.getNextId(), String.class);
            if (nextIdList.size() > 1) {
                dummyNodeStack.push(dummyNode);
            } else if (prevIdList.size() > 1) {
                if (dummyNodeStack.size() == 1) {
                    break;
                }
                dummyNodeStack.pop();
            }
        }
        //此时 dummyNode 为 虚拟节点(发散节点) 所对应的 虚拟节点(聚合节点)
        //获取 虚拟节点(聚合节点) 的父节点集合
        List<String> prevIdList = JSON.parseArray(dummyNode.getPrevId(), String.class);

        //分两种情况处理
        HashSet<String> deleteNodeIdList = new HashSet<>();
        if (nextNodeIdList.size() > 2) {//子节点数量大于2
            //删除条件
            Integer priority = conditionGroupList.get(0).getPriority();
            appConditionGroupMapper.delete(new LambdaQueryWrapper<AppConditionGroup>()
                    .eq(AppConditionGroup::getNodeId, node.getId()));
            appConditionMapper.delete(new LambdaQueryWrapper<AppCondition>()
                    .eq(AppCondition::getNodeId, node.getId()));
            //修改其他条件的优先级
            List<AppConditionGroup> updateConditionGroup = appConditionGroupMapper.selectList(new LambdaQueryWrapper<AppConditionGroup>()
                    .eq(AppConditionGroup::getPrevNodeId, dummyNodeUp.getId())
                    .ge(AppConditionGroup::getPriority, priority));
            for (AppConditionGroup appConditionGroup : updateConditionGroup) {
                appConditionGroup.setPriority(appConditionGroup.getPriority() - 1);
                appConditionGroupMapper.updateById(appConditionGroup);
            }

            //修改节点关系
            nextNodeIdList.remove(node.getId());
            dummyNodeUp.setNextId(JSON.toJSONString(nextNodeIdList));
            appProcessNodeConfigMapper.updateById(dummyNodeUp);
            //收集需删除节点
            getIdList(node, dummyNode, deleteNodeIdList);
            //修改节点关系
            prevIdList.removeAll(deleteNodeIdList);
            dummyNode.setPrevId(JSON.toJSONString(prevIdList));
            appProcessNodeConfigMapper.updateById(dummyNode);
        } else {//子节点数量小于等于2
            //获取 虚拟节点(发散节点) 的 上一个节点
            AppProcessNodeConfig dummyNodeUpPrevNode = getPrevNodeList(dummyNodeUp).get(0);
            //获取 虚拟节点(聚合节点) 的 下一个节点
            AppProcessNodeConfig dummyNodeDownNextNode = getNextNode(dummyNode);
            //修改节点关系
            dummyNodeUpPrevNode.setNextId(JSON.toJSONString(Arrays.asList(dummyNodeDownNextNode.getId())));
            appProcessNodeConfigMapper.updateById(dummyNodeUpPrevNode);
            dummyNodeDownNextNode.setPrevId(JSON.toJSONString(Arrays.asList(dummyNodeUpPrevNode.getId())));
            appProcessNodeConfigMapper.updateById(dummyNodeDownNextNode);

            //收集需删除节点
            getIdList(dummyNodeUp, dummyNodeDownNextNode, deleteNodeIdList);

            //删除条件
            appConditionGroupMapper.delete(new LambdaQueryWrapper<AppConditionGroup>()
                    .in(AppConditionGroup::getNodeId, deleteNodeIdList));
            appConditionMapper.delete(new LambdaQueryWrapper<AppCondition>()
                    .in(AppCondition::getNodeId, deleteNodeIdList));
        }

        //删除节点
        appProcessNodeConfigMapper.deleteBatchIds(deleteNodeIdList);

        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult saveConditionNode(ConditionNodeInfoReq req) {
        //校验参数
        if (isNullOrEmpty(req.getId(), req.getName(), req.getPriority(), req.getAppConditionGroupList())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<AppConditionGroupReq> conditionGroupReqList = req.getAppConditionGroupList();
        for (AppConditionGroupReq appConditionGroupReq : conditionGroupReqList) {
            if (isNullOrEmpty(appConditionGroupReq.getIsDefault(), appConditionGroupReq.getConditionList())) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
            for (AppConditionReq appConditionReq : appConditionGroupReq.getConditionList()) {
                if (isNullOrEmpty(appConditionReq.getControlSubType(), appConditionReq.getIsSubmitter(), appConditionReq.getSort())) {
                    return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
                }
                if (!(appConditionReq.getIsSubmitter() && appConditionReq.getControlSubType().equals(CONDITION_SUBTYPE_SUBMITTER))) {
                    if (isNullOrEmpty(appConditionReq.getControlOrderId(), appConditionReq.getControlName())) {
                        return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
                    }
                }
                if (!appConditionReq.getControlSubType().equals(CONDITION_SUBTYPE_SWITCH)) {
                    if (isNullOrEmpty(appConditionReq.getData())) {
                        return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
                    }
                }
            }
        }

        //校验条件节点是否存在
        AppProcessNodeConfig conditionNode = appProcessNodeConfigMapper.selectById(req.getId());
        if (isNullOrEmpty(conditionNode) || conditionNode.getType() != CONDITION_NODE) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }

        //修改节点名
        conditionNode.setName(req.getName());
        appProcessNodeConfigMapper.updateById(conditionNode);

        //删除原先条件组
        appConditionGroupMapper.delete(new LambdaQueryWrapper<AppConditionGroup>()
                .eq(AppConditionGroup::getNodeId, req.getId()));
        //删除原先条件
        appConditionMapper.delete(new LambdaQueryWrapper<AppCondition>()
                .eq(AppCondition::getNodeId, req.getId()));

        //保存 条件组 及 条件
        for (AppConditionGroupReq appConditionGroupReq : conditionGroupReqList) {
            AppConditionGroup conditionGroup = new AppConditionGroup();
            BeanUtils.copyProperties(appConditionGroupReq, conditionGroup);
            conditionGroup.setPrevNodeId(JSON.parseArray(conditionNode.getPrevId(), String.class).get(0));
            conditionGroup.setNodeId(conditionNode.getId());
            conditionGroup.setPriority(req.getPriority());
            //插入新条件组
            appConditionGroupMapper.insert(conditionGroup);

            //保存条件
            for (AppConditionReq appConditionReq : appConditionGroupReq.getConditionList()) {
                //校验控件顺序表中该控件是否存在
                if (!(appConditionReq.getIsSubmitter() && appConditionReq.getControlSubType().equals(CONDITION_SUBTYPE_SUBMITTER))) {
                    if (isNullOrEmpty(appControlOrderMapper.selectById(appConditionReq.getControlOrderId()))) {
                        return CommonResult.failed(CommonCodeEnum.CONTROL_ORDER_NOT_EXIST);
                    }
                }
                AppCondition condition = new AppCondition();
                BeanUtils.copyProperties(appConditionReq, condition);
                condition.setNodeId(conditionNode.getId());
                condition.setConditionGroupId(conditionGroup.getId());
                //插入条件
                appConditionMapper.insert(condition);
            }
        }

        return CommonResult.success();
    }

    /**
     * 获取传入的 两个节点之间 所有节点的id集合 [左闭右开]
     *
     * @param node       当前节点
     * @param targetNode 目标节点
     * @param nodeIdList 用来存放id的集合
     */
    private void getIdList(AppProcessNodeConfig node, AppProcessNodeConfig targetNode, HashSet<String> nodeIdList) {
        if (!node.getId().equals(targetNode.getId())) {
            //把当前 节点id 存入 nodeIdList
            nodeIdList.add(node.getId());
            //递归向下查找
            for (AppProcessNodeConfig nextNode : getNextNodeList(node)) {
                getIdList(nextNode, targetNode, nodeIdList);
            }
        }
    }

    /**
     * 获取下一个节点
     *
     * @param node 当前节点
     * @return 下一个节点
     */
    private AppProcessNodeConfig getNextNode(AppProcessNodeConfig node) {
        String nextId = JSON.parseArray(node.getNextId(), String.class).get(0);
        return appProcessNodeConfigMapper.selectById(nextId);
    }

    /**
     * 获取子节点集合
     *
     * @param node 当前节点
     * @return 子节点集合
     */
    private List<AppProcessNodeConfig> getNextNodeList(AppProcessNodeConfig node) {
        return appProcessNodeConfigMapper.selectBatchIds(JSON.parseArray(node.getNextId(), String.class));
    }

    /**
     * 获取父节点集合
     *
     * @param node 当前节点
     * @return 父节点集合
     */
    private List<AppProcessNodeConfig> getPrevNodeList(AppProcessNodeConfig node) {
        return appProcessNodeConfigMapper.selectBatchIds(JSON.parseArray(node.getPrevId(), String.class));
    }

    @Override
    @Transactional
    public ResponseResult addBranch(AddBranchReq addBranchReq) {
        //校验参数
        if (isNullOrEmpty(addBranchReq.getPrevNodeId()) || isNullOrEmpty(addBranchReq.getNextNodeId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        String prevNodeId = addBranchReq.getPrevNodeId();
        String nextNodeId = addBranchReq.getNextNodeId();

        //获取父节点及其子idList
        AppProcessNodeConfig prevNode = appProcessNodeConfigMapper.selectById(prevNodeId);
        if (isNullOrEmpty(prevNode)) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        List<String> prevNodeSonIdList = JSON.parseArray(prevNode.getNextId(), String.class);
        //校验传入父子节点是否相邻
        if (!(prevNodeSonIdList.contains(nextNodeId))) {
            return CommonResult.failed(CommonCodeEnum.PREV_AND_NEXT_NODE_NOT_NEAR);
        }
        //获取子节点及其父idList
        AppProcessNodeConfig nextNode = appProcessNodeConfigMapper.selectById(nextNodeId);
        if (isNullOrEmpty(nextNode)) {
            return CommonResult.failed(CommonCodeEnum.NODE_NOT_EXIST);
        }
        List<String> nextNodeFatherIdList = JSON.parseArray(nextNode.getPrevId(), String.class);
        //校验传入父子节点是否相邻
        if (!(nextNodeFatherIdList.contains(prevNodeId))) {
            return CommonResult.failed(CommonCodeEnum.PREV_AND_NEXT_NODE_NOT_NEAR);
        }

        //创建分支节点(6个:上下虚拟节点及两个条件节点和审批节点)并处理节点间关系
        AppProcessNodeConfig approverLeft = initializeApproverNode(prevNode.getAppId());
        AppProcessNodeConfig approverRight = initializeApproverNode(prevNode.getAppId());
        AppProcessNodeConfig dummyUp = initializeDummyNode(prevNode.getAppId());
        AppProcessNodeConfig dummyDown = initializeDummyNode(prevNode.getAppId());
        AppProcessNodeConfig conditionLeft = initializeConditionNode(prevNode.getAppId(), dummyUp.getId(), 1, false);
        AppProcessNodeConfig conditionRight = initializeConditionNode(prevNode.getAppId(), dummyUp.getId(), 2, true);

        //设置条件节点父子节点
        conditionLeft.setPrevId(JSON.toJSONString(Arrays.asList(dummyUp.getId())));
        conditionLeft.setNextId(JSON.toJSONString(Arrays.asList(approverLeft.getId())));
        conditionRight.setPrevId(JSON.toJSONString(Arrays.asList(dummyUp.getId())));
        conditionRight.setNextId(JSON.toJSONString(Arrays.asList(approverRight.getId())));

        //设置审批节点父子节点
        approverLeft.setPrevId(JSON.toJSONString(Arrays.asList(conditionLeft.getId())));
        approverLeft.setNextId(JSON.toJSONString(Arrays.asList(dummyDown.getId())));
        approverRight.setPrevId(JSON.toJSONString(Arrays.asList(conditionRight.getId())));
        approverRight.setNextId(JSON.toJSONString(Arrays.asList(dummyDown.getId())));

        //设置虚拟节点父子节点
        dummyUp.setPrevId(JSON.toJSONString(Arrays.asList(prevNodeId)));
        dummyUp.setNextId(JSON.toJSONString(Arrays.asList(conditionLeft.getId(), conditionRight.getId())));
        dummyDown.setPrevId(JSON.toJSONString(Arrays.asList(approverLeft.getId(), approverRight.getId())));
        dummyDown.setNextId(JSON.toJSONString(Arrays.asList(nextNodeId)));

        //更新节点关系
        ArrayList<AppProcessNodeConfig> nodeList = new ArrayList<>();
        nodeList.add(conditionLeft);
        nodeList.add(conditionRight);
        nodeList.add(approverLeft);
        nodeList.add(approverRight);
        nodeList.add(dummyUp);
        nodeList.add(dummyDown);

        //修改父节点
        Collections.replaceAll(prevNodeSonIdList, nextNodeId, dummyUp.getId());
        prevNode.setNextId(JSON.toJSONString(prevNodeSonIdList));
        nodeList.add(prevNode);
        //修改子节点
        Collections.replaceAll(nextNodeFatherIdList, prevNodeId, dummyDown.getId());
        nextNode.setPrevId(JSON.toJSONString(nextNodeFatherIdList));
        nodeList.add(nextNode);

        this.updateBatchById(nodeList);
        return CommonResult.success();
    }

    /**
     * 初始化审批人节点
     *
     * @param appId 应用id
     * @return 审批人节点
     */
    private AppProcessNodeConfig initializeApproverNode(String appId) {
        AppProcessNodeConfig approver = new AppProcessNodeConfig();
        approver.setAppId(appId);
        approver.setName("审批");
        approver.setType(APPROVE_NODE);
        approver.setApproverType(DYNAMIC_APP_SUBMITTER_CHOOSE);
        //设置权限
        String authority = getAuthority(appId, true, true);
        approver.setAuthority(authority);
        approver.setIsDeliver(true);
        approver.setIsRollback(true);
        approver.setIsSign(true);
        approver.setIsStamp(false);
        approver.setIsOpinion(true);
        appProcessNodeConfigMapper.insert(approver);
        return approver;
    }

    /**
     * 初始化条件节点
     *
     * @param appId 应用Id
     * @return 条件节点
     */
    private AppProcessNodeConfig initializeConditionNode(String appId, String prevNodeId, Integer priority, Boolean isDefault) {
        //创建条件节点
        AppProcessNodeConfig condition = new AppProcessNodeConfig();
        condition.setAppId(appId);
        condition.setName("条件" + priority);
        condition.setType(CONDITION_NODE);
        condition.setIsDeliver(false);
        condition.setIsRollback(false);
        condition.setIsSign(false);
        condition.setIsStamp(false);
        condition.setIsOpinion(false);
        appProcessNodeConfigMapper.insert(condition);

        //创建条件组
        AppConditionGroup conditionGroup = new AppConditionGroup();
        conditionGroup.setNodeId(condition.getId());
        conditionGroup.setPrevNodeId(prevNodeId);
        conditionGroup.setPriority(priority);
        conditionGroup.setIsDefault(isDefault);
        appConditionGroupMapper.insert(conditionGroup);

        return condition;
    }

    /**
     * 初始化抄送人节点
     *
     * @param appId 应用id
     * @return 抄送人节点
     */
    private AppProcessNodeConfig initializeCopyNode(String appId) {
        AppProcessNodeConfig copyNode = new AppProcessNodeConfig();
        copyNode.setAppId(appId);
        copyNode.setName("抄送");
        copyNode.setType(COPY_NODE);
        copyNode.setApproverType(DYNAMIC_APP_SUBMITTER_CHOOSE);
        //设置权限
        String authority = getAuthority(appId, true, false);
        copyNode.setAuthority(authority);
        copyNode.setIsDeliver(true);
        copyNode.setIsRollback(true);
        copyNode.setIsSign(true);
        copyNode.setIsStamp(false);
        copyNode.setIsOpinion(true);
        appProcessNodeConfigMapper.insert(copyNode);
        return copyNode;
    }

    /**
     * 初始化虚拟节点
     *
     * @param appId 应用id
     * @return 虚拟节点
     */
    private AppProcessNodeConfig initializeDummyNode(String appId) {
        AppProcessNodeConfig dummy = new AppProcessNodeConfig();
        dummy.setAppId(appId);
        dummy.setName("虚拟节点");
        dummy.setType(DUMMY_NODE);
        dummy.setIsDeliver(false);
        dummy.setIsRollback(false);
        dummy.setIsSign(false);
        dummy.setIsStamp(false);
        dummy.setIsOpinion(false);
        appProcessNodeConfigMapper.insert(dummy);
        return dummy;
    }

    /**
     * 初始化应用流程
     *
     * @param appId 应用id
     * @return 应用流程集合
     */
    private List<AppProcessNodeConfig> initializeProcessNode(String appId) {
        ArrayList<AppProcessNodeConfig> nodeList = new ArrayList<>();
        //初始化提交节点
        AppProcessNodeConfig applicant = new AppProcessNodeConfig();
        applicant.setAppId(appId);
        applicant.setName("提交");
        applicant.setType(APPLICANT_NODE);
        applicant.setApproverType(DYNAMIC_APP_SUBMITTER);
        //设置权限
        String applicantAuthority = getAuthority(appId, true, true);
        applicant.setAuthority(applicantAuthority);
        applicant.setIsDeliver(false);
        applicant.setIsRollback(false);
        applicant.setIsSign(false);
        applicant.setIsStamp(false);
        applicant.setIsOpinion(false);
        appProcessNodeConfigMapper.insert(applicant);

        //初始化审批节点
        AppProcessNodeConfig approver = initializeApproverNode(appId);

        //初始化结束节点
        AppProcessNodeConfig endNode = new AppProcessNodeConfig();
        endNode.setAppId(appId);
        endNode.setName("结束");
        endNode.setType(END_NODE);
        //设置权限
        String endNodeAuthority = getAuthority(appId, false, false);
        endNode.setAuthority(endNodeAuthority);
        endNode.setIsDeliver(false);
        endNode.setIsRollback(false);
        endNode.setIsSign(false);
        endNode.setIsStamp(false);
        endNode.setIsOpinion(false);
        appProcessNodeConfigMapper.insert(endNode);

        //处理节点关系
        ArrayList<String> applicantNextId = new ArrayList<>();
        applicantNextId.add(approver.getId());
        applicant.setNextId(JSON.toJSONString(applicantNextId));
        appProcessNodeConfigMapper.updateById(applicant);
        nodeList.add(applicant);

        ArrayList<String> approverPrevId = new ArrayList<>();
        ArrayList<String> approverNextId = new ArrayList<>();
        approverPrevId.add(applicant.getId());
        approverNextId.add(endNode.getId());
        approver.setPrevId(JSON.toJSONString(approverPrevId));
        approver.setNextId(JSON.toJSONString(approverNextId));
        appProcessNodeConfigMapper.updateById(approver);
        nodeList.add(approver);

        ArrayList<String> endNodePrevId = new ArrayList<>();
        endNodePrevId.add(approver.getId());
        endNode.setPrevId(JSON.toJSONString(endNodePrevId));
        appProcessNodeConfigMapper.updateById(endNode);
        nodeList.add(endNode);

        return nodeList;
    }

    /**
     * 获取节点权限
     *
     * @param appId 应用id
     * @return 节点权限字符串
     */
    public String getAuthority(String appId, Boolean readable, Boolean editable) {
        List<AuthorityResp> authorityRespList = new ArrayList<>();
        List<AppControlOrder> controlOrderList = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>().eq(AppControlOrder::getAppId, appId));
        for (AppControlOrder controlOrder : controlOrderList) {
            if (Arrays.asList(TextControlType).contains(controlOrder.getType())) {
                AppTextControlConfig textControlConfig = appTextControlConfigMapper.selectList(new LambdaQueryWrapper<AppTextControlConfig>()
                        .eq(AppTextControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(textControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            } else if (Arrays.asList(TimeControlType).contains(controlOrder.getType())) {
                AppTimeControlConfig timeControlConfig = appTimeControlConfigMapper.selectList(new LambdaQueryWrapper<AppTimeControlConfig>()
                        .eq(AppTimeControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(timeControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            } else if (Arrays.asList(OptionControlType).contains(controlOrder.getType())) {
                AppOptionControlConfig optionControlConfig = appOptionControlConfigMapper.selectList(new LambdaQueryWrapper<AppOptionControlConfig>()
                        .eq(AppOptionControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(optionControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            } else if (Arrays.asList(StepperControlType).contains(controlOrder.getType())) {
                AppStepperControlConfig stepperControlConfig = appStepperControlConfigMapper.selectList(new LambdaQueryWrapper<AppStepperControlConfig>()
                        .eq(AppStepperControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(stepperControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            } else if (Arrays.asList(FileControlType).contains(controlOrder.getType())) {
                AppFileControlConfig fileControlConfig = appFileControlConfigMapper.selectList(new LambdaQueryWrapper<AppFileControlConfig>()
                        .eq(AppFileControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(fileControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            }
        }
        return JSON.toJSONString(authorityRespList);
    }

}
